package com.design.working.service.impl;

import com.design.working.dao.StudentDAO;
import com.design.working.dao.impl.StudentDAOImpl;
import com.design.working.dto.PageControlDTO;
import com.design.working.dto.StudentWorkApplicationDTO;
import com.design.working.dto.StudentWorkMotionDTO;
import com.design.working.dto.student.UpdateIndividualInformationDTO;
import com.design.working.po.student.SelectAllWorkPO;
import com.design.working.po.student.StudentSalaryPO;
import com.design.working.service.StudentService;
import com.design.working.vo.*;
import com.design.working.vo.student.*;
import com.design.working.po.student.StudentWorkApplicationRecordPO;

import java.util.ArrayList;
import java.util.List;

import static com.design.working.enums.OperationEnum.NO_PAGE;

/**
 * 学生端服务层
 *
 * @author gavin
 * @date 2022/5/9 12:02
 */
public class StudentServiceImpl implements StudentService {

    /**
     * 不是贫困生, "0"
     */
    private static final String NOT_NEED_STUDENT = "0";

    /**
     * 是贫困生, "1"
     */
    private static final String NEED_STUDENT = "1";

    /**
     * 性别男, "M"
     */
    private static final String MALE = "M";

    /**
     * 性别女, "F"
     */
    private static final String FEMALE = "F";

    /**
     * 成功, 1
     */
    private static final int SUCCESS = 1;

    /**
     * 秋季学期, 1
     */
    private static final String AUTUMN_TERM = "1";

    /**
     * 未处理, 0
     */
    private static final int NOT_HANDLED = 0;

    /**
     * 拒绝（负数表示）, -1
     */
    private static final int REJECT_IN_MINUS = -1;

    /**
     * 批准, 1
     */
    private static final int APPROVED = 1;

    /**
     * 未发放, 0
     */
    private static final int NOT_PAY = 0;

    /**
     * 已发放, 1
     */
    private static final int PAID = 1;

    /**
     * 一月, 1
     */
    private static final int JANUARY = 1;

    /**
     * 十二月, 12
     */
    private static final int DECEMBER = 12;

    private final StudentDAO studentDAO = new StudentDAOImpl();

    /**
     * （申请岗位）查找全部岗位
     *
     * @param place          需要查询的岗位所属的行政分类
     * @param uid            学号
     * @param pageControlDTO 分页参数
     * @return 查询到的岗位
     */
    @Override
    public ResponseVO<List<JobSelectionVO>> selectJob(String place, Integer uid, PageControlDTO pageControlDTO) {

        // 将前端参数转为数据库对应字段的值
        String substitution = null;
        switch (place) {
            case "classes":
                substitution = "教学区";
                break;
            case "admin":
                substitution = "行政区";
                break;
            case "edu":
                substitution = "教务";
                break;
            case "library":
                substitution = "图书馆";
                break;
            case "service":
                substitution = "后勤";
                break;
            default:
        }

        int size = studentDAO.selectJob(substitution, NO_PAGE.getVal(), pageControlDTO.getLimit()).size();
        if (size == 0) {
            return ResponseVO.error("暂时没有岗位！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<JobSelectionVO> list = studentDAO.selectJob(substitution, page, pageControlDTO.getLimit());

        // 设置是否可选
        if (!isStatus(uid)) {
            //所有岗位都设为不可选
            for (JobSelectionVO jobSelectionVO : list) {
                jobSelectionVO.setChoice(0);
            }
        }
        return ResponseVO.success(size, list);
    }

    /**
     * @param uid 判断岗位是否可选
     * @return true-可选; false-不可选
     */
    private boolean isStatus(Integer uid) {
        // 后期可对这两个方法改写。比如改成list接收，扩展出可以查几种岗位状态
        return isStudentInWorking(uid) && isStudentInApplying(uid);
    }

    /**
     * 查询学生是否有工作
     *
     * @param uid 学生学号
     * @return true-有; false-没有
     */
    private boolean isStudentInWorking(Integer uid) {
        return studentDAO.isStudentInWorking(uid);
    }

    /**
     * 查询学生是否有在申请的岗位
     *
     * @param uid 学生学号
     * @return true-有; false-没有
     */
    private boolean isStudentInApplying(Integer uid) {
        return studentDAO.isStudentInApplying(uid);
    }

    /**
     * 插入学生申请的岗位
     *
     * @param uid                       学生学号
     * @param studentWorkApplicationDTO 前端表单传来的信息
     * @return 成功
     */
    @Override
    public int insertStudentWorkApplication(Integer uid, StudentWorkApplicationDTO studentWorkApplicationDTO) {
        studentDAO.insertStudentWorkApplication(uid, studentWorkApplicationDTO);
        return SUCCESS;
    }

    /**
     * 查询学生目前在岗岗位
     *
     * @param uid 要查询的学生学号
     * @return 学生在岗岗位
     */
    @Override
    public ResponseVO<List<StudentCurrentWorkVO>> selectCurrentWork(Integer uid) {

        List<StudentCurrentWorkVO> list = studentDAO.selectCurrentWork(uid);
        // 如果学生目前没有在岗岗位
        if (list.isEmpty()) {
            return ResponseVO.error("暂时没有岗位！");
        }
        return ResponseVO.success(list.size(), list);
    }

    /**
     * 查询学生所有的用工岗位
     *
     * @param uid            学生学号
     * @param pageControlDTO 分页参数
     * @return 学生所有的用工岗位
     */
    @Override
    public ResponseVO<List<StudentAttendanceVO>> selectAllWork(Integer uid, PageControlDTO pageControlDTO) {

        int size = studentDAO.selectAllWork(uid, NO_PAGE.getVal(), pageControlDTO.getLimit()).size();
        // 如果没有用工记录
        if (size == 0) {
            return ResponseVO.error("暂时没有岗位！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<SelectAllWorkPO> temp = studentDAO.selectAllWork(uid, page, pageControlDTO.getLimit());

        List<StudentAttendanceVO> list = new ArrayList<>();

        // 转换数据
        for (SelectAllWorkPO selectAllWorkPO : temp) {
            StudentAttendanceVO studentAttendanceVO = new StudentAttendanceVO();
            studentAttendanceVO.setId(selectAllWorkPO.getId());
            studentAttendanceVO.setDept(selectAllWorkPO.getDept());
            studentAttendanceVO.setWork(selectAllWorkPO.getWork());
            studentAttendanceVO.setDetail(selectAllWorkPO.getDetail());

            // 学期转换
            String year = selectAllWorkPO.getYear() + "-" + (selectAllWorkPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(selectAllWorkPO.getTerm())) ? "秋冬学期" : "春夏学期";

            studentAttendanceVO.setYear(year);
            studentAttendanceVO.setTerm(term);

            list.add(studentAttendanceVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 查询学生的考勤记录
     *
     * @param id             student_to_work的id
     * @param pageControlDTO 分页参数
     * @return 对应工作的考勤记录
     */
    @Override
    public ResponseVO<List<StudentWorkAttendanceVO>> selectAttendanceById(Integer id, PageControlDTO pageControlDTO) {

        int size = studentDAO.selectAttendanceById(id, NO_PAGE.getVal(), pageControlDTO.getLimit()).size();
        // 如果没有考勤记录
        if (size == 0) {
            return ResponseVO.error("暂时没有你的考勤记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<StudentWorkAttendanceVO> list = studentDAO.selectAttendanceById(id, page, pageControlDTO.getLimit());

        return ResponseVO.success(size, list);
    }

    /**
     * 插入学生申请岗位变更数据
     *
     * @param studentWorkMotionDTO 前端传来的申请数据
     * @return 成功
     */
    @Override
    public int insertStudentWorkMotion(StudentWorkMotionDTO studentWorkMotionDTO) {
        studentDAO.insertStudentWorkMotion(studentWorkMotionDTO);
        return SUCCESS;
    }

    /**
     * 查询学生所有的岗位记录
     *
     * @param uid            学生学号
     * @param pageControlDTO 分页参数
     * @return 该生的所有申请岗位记录
     */
    @Override
    public ResponseVO<List<StudentWorkApplicationRecordVO>> selectAllWorkApplication(Integer uid, PageControlDTO pageControlDTO) {

        int size = studentDAO.selectAllWorkApplication(uid, NO_PAGE.getVal(), pageControlDTO.getLimit()).size();
        if (size == 0) {
            return ResponseVO.error("暂时没有你的申请记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<StudentWorkApplicationRecordPO> temp = studentDAO.selectAllWorkApplication(uid, page, pageControlDTO.getLimit());

        List<StudentWorkApplicationRecordVO> list = new ArrayList<>();

        for (StudentWorkApplicationRecordPO studentWorkApplicationRecordPO : temp) {

            StudentWorkApplicationRecordVO studentWorkApplicationRecordVO = new StudentWorkApplicationRecordVO();

            String year = studentWorkApplicationRecordPO.getYear() + "-" + (studentWorkApplicationRecordPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(studentWorkApplicationRecordPO.getTerm())) ? "秋冬学期" : "春夏学期";
            String counselorStatus = null;
            String deptStatus = null;
            String adminStatus = null;
            switch (studentWorkApplicationRecordPO.getCounselorStatus()) {
                case NOT_HANDLED:
                    counselorStatus = "未处理";
                    break;
                case REJECT_IN_MINUS:
                    counselorStatus = "拒绝";
                    break;
                case APPROVED:
                    counselorStatus = "通过";
                    break;
                default:
            }
            switch (studentWorkApplicationRecordPO.getDeptStatus()) {
                case NOT_HANDLED:
                    deptStatus = "未处理";
                    break;
                case REJECT_IN_MINUS:
                    deptStatus = "拒绝";
                    break;
                case APPROVED:
                    deptStatus = "通过";
                    break;
                default:
            }
            switch (studentWorkApplicationRecordPO.getAdminStatus()) {
                case NOT_HANDLED:
                    adminStatus = "未处理";
                    break;
                case REJECT_IN_MINUS:
                    adminStatus = "拒绝";
                    break;
                case APPROVED:
                    adminStatus = "通过";
                    break;
                default:
            }
            String attitude = "辅导员意见：" + studentWorkApplicationRecordPO.getCounselorText() + "  "
                    + "用工部门意见：" + studentWorkApplicationRecordPO.getDeptText() + "  "
                    + "管理员意见：" + studentWorkApplicationRecordPO.getAdminText();

            studentWorkApplicationRecordVO.setYear(year);
            studentWorkApplicationRecordVO.setTerm(term);
            studentWorkApplicationRecordVO.setCounselorStatus(counselorStatus);
            studentWorkApplicationRecordVO.setDeptStatus(deptStatus);
            studentWorkApplicationRecordVO.setAdminStatus(adminStatus);
            studentWorkApplicationRecordVO.setAttitude(attitude);
            studentWorkApplicationRecordVO.setWork(studentWorkApplicationRecordPO.getWork());
            studentWorkApplicationRecordVO.setDept(studentWorkApplicationRecordPO.getDept());
            studentWorkApplicationRecordVO.setDetail(studentWorkApplicationRecordPO.getDetail());

            list.add(studentWorkApplicationRecordVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 查询学生工资
     *
     * @param uid            学生学号
     * @param type           操作：lastMonth-查询上月工资；all-查询历史全部工资
     * @param pageControlDTO 分页参数
     * @return 学生工资信息
     * SQL异常
     */
    @Override
    public ResponseVO<List<StudentSalaryVO>> selectSalary(Integer uid, String type, PageControlDTO pageControlDTO) {

        int size = studentDAO.selectSalary(uid, type, NO_PAGE.getVal(), pageControlDTO.getLimit()).size();
        if (size == 0) {
            return ResponseVO.error("您暂时没有工资记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<StudentSalaryPO> temp = studentDAO.selectSalary(uid, type, page, pageControlDTO.getLimit());

        List<StudentSalaryVO> list = new ArrayList<>();

        // 数据转换
        for (StudentSalaryPO studentSalaryPO : temp) {

            StudentSalaryVO studentSalaryVO = new StudentSalaryVO();

            // 学期转换
            String year = studentSalaryPO.getYear() + "-" + (studentSalaryPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(studentSalaryPO.getTerm())) ? "秋冬学期" : "春夏学期";

            String date;

            // 月份转换
            if (studentSalaryPO.getDate().getMonthValue() != JANUARY) {
                date = studentSalaryPO.getDate().getYear() + "-" + (studentSalaryPO.getDate().getMonthValue() - 1);
            } else {
                date = (studentSalaryPO.getDate().getYear() - 1) + "-" + DECEMBER;
            }

            // 发放工资转换
            String isSettled = null;
            switch (studentSalaryPO.getSettled()) {
                case NOT_PAY:
                    isSettled = "未发放";
                    break;
                case PAID:
                    isSettled = "已发放";
                    break;
                default:
            }

            studentSalaryVO.setYear(year);
            studentSalaryVO.setTerm(term);
            studentSalaryVO.setDept(studentSalaryPO.getDept());
            studentSalaryVO.setSalary(studentSalaryPO.getSalary());
            studentSalaryVO.setName(studentSalaryPO.getName());
            studentSalaryVO.setDate(date);
            studentSalaryVO.setSettled(isSettled);

            list.add(studentSalaryVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 通过学号查询个人信息
     *
     * @param uid 学号
     * @return 相关的个人信息
     */
    @Override
    public ResponseVO<IndividualInformationByUidVO> selectIndividualInformationByUid(Integer uid) {

        IndividualInformationByUidVO individualInformationByUidVO = studentDAO.selectIndividualInformationByUid(uid);

        // 处理性别
        switch (individualInformationByUidVO.getGender()) {
            case MALE:
                individualInformationByUidVO.setGender("男");
                break;
            case FEMALE:
                individualInformationByUidVO.setGender("女");
                break;
            default:
        }
        // 处理贫困生状态
        switch (individualInformationByUidVO.getNeedyStudent()) {
            case NOT_NEED_STUDENT:
                individualInformationByUidVO.setNeedyStudent("否");
                break;
            case NEED_STUDENT:
                individualInformationByUidVO.setNeedyStudent("是");
                break;
            default:
        }
        return ResponseVO.success(individualInformationByUidVO);
    }

    /**
     * 通过学生学号uid更新个人信息
     *
     * @param uid                            学生学号
     * @param updateIndividualInformationDTO 更新的数据
     * @return 成功
     */
    @Override
    public int updateIndividualInformationByUid(Integer uid, UpdateIndividualInformationDTO updateIndividualInformationDTO) {
        studentDAO.updateIndividualInformationByUid(uid, updateIndividualInformationDTO);
        return SUCCESS;
    }
}